package kr.pfg.applications.captureyourscreen.display
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.NativeWindowType;
	import flash.display.Screen;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	
	import kr.as3.display.NewWindow;
	import kr.pfg.applications.captureyourscreen.events.CaptureEvent;

	public class ImageWindow extends Sprite
	{
		
		private var bmp:Bitmap;
		
		public function ImageWindow( bmd:BitmapData )
		{
			super();
			
			this.bmp = new Bitmap( bmd );
			this.addChild( this.bmp );
			this.newWindow();
		
           //fullscreenMode		
			this.isFullscreenSize()
			
			this.createCaptureFrame();
		}
		
		private var window:NewWindow;
		private function newWindow():void
		{
			this.window = 
			new NewWindow( NativeWindowType.NORMAL, NativeWindowSystemChrome.STANDARD, false, false, false, false );
//			new NewWindow( NativeWindowType.NORMAL, NativeWindowSystemChrome.NONE, false, false, false, false );
			window.width = this.bmp.width;
			window.height = this.bmp.height + 22;
			window.title = "Captured Image";
			window.stage.addChild( this );
			window.activate()
			
			if( this.isFullscreenSize() )
				window.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
		}
		
		// wooyaggo : check whether captured image is fullscreen captured image.
		// nike : Screen.mainScreen.bounds in Mac is smaller than Window -1 pixel.  
		private function isFullscreenSize(): Boolean
		{
			var bound: Rectangle = Screen.mainScreen.bounds;
			trace( bound.width, bound.height );
			if( this.bmp.width != bound.width && this.bmp.width + 1 != bound.width )
				return false;
					
			if( this.bmp.height != bound.height && this.bmp.height + 1 != bound.height )
				return false;
					 
			return true;
		}
		
		private var editWindow:EditWindow;
		private var captureFrame: CaptureFrame;
		private function createCaptureFrame(): void
		{
			this.captureFrame = new CaptureFrame( this );
			trace( this.captureFrame.region );
			this.captureFrame.width = 200;
			this.captureFrame.height = 300;
			this.captureFrame.x = int( ( this.width - this.captureFrame.width ) / 2 );
			this.captureFrame.y = int( ( this.height - this.captureFrame.height ) / 2 );
			this.captureFrame.addEventListener( CaptureEvent.CAPTURE_FULL, this.fullScreenCaptureHandler );
			this.captureFrame.addEventListener( CaptureEvent.CAPTURE_REGION, this.standardCaptureHandler );
			
			this.addChild( this.captureFrame );
		}
		
		// nike : When full screen capture event dispatched..
		private function fullScreenCaptureHandler( e:CaptureEvent ):void
		{
			trace( "full" );
			this.createEditWindow();
			this.closeCaptureFrame();
			
			this.editWindow.fullScreenMode = true;
			this.editWindow.bitmap = this.bmp.bitmapData;
			this.editWindow.activate();
			this.editWindow.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
		}
		
		// nike : When standard screen capture event dispatched..
		private function standardCaptureHandler( e:CaptureEvent ):void
		{
			trace( "standard!" );
			this.createEditWindow();
			this.closeCaptureFrame();
			
			var capturedBmd:BitmapData = new BitmapData( this.captureFrame.border.width, this.captureFrame.border.height );
			var matrix:Matrix = new Matrix();
			matrix.translate( -this.captureFrame.x, -this.captureFrame.y );
			capturedBmd.draw( this.bmp.bitmapData, matrix );
			
			this.editWindow.stage.displayState = StageDisplayState.NORMAL;
			this.editWindow.bitmap = capturedBmd;
			this.editWindow.x = this.captureFrame.x - 3;
			this.editWindow.y = this.captureFrame.y - 25;
			this.editWindow.activate();
		}
		
		private function createEditWindow():void
		{
			this.editWindow = 
			new EditWindow( NativeWindowType.NORMAL, NativeWindowSystemChrome.NONE,false, true, false, false );
			this.editWindow
		}
		
		private function closeCaptureFrame():void
		{
			this.window.close();
			this.window.stage.removeChild( this );
			this.removeChild( this.bmp );
			this.removeChild( this.captureFrame );
			this.window.stage.displayState = StageDisplayState.NORMAL;
		}
	}
}